/* 
    cnpm i mini-css-extract-plugin -D
    可以提取css

    cnpm i optimize-css-assets-webpack-plugin -D
    cnpm i css-minimizer-webpack-plugin -D
    可以压缩css

*/
const {resolve} = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

// 提取css文件插件
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

// 压缩css插件
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');
const CssMinimizerWebpackPlugin = require('css-minimizer-webpack-plugin');

// 设置nodejs环境变量
process.env.NODE_ENV = 'development';

module.exports = {
    entry: './src/js/index.js',
    output: {
        filename: 'js/built.js',
        path: resolve(__dirname, 'build')
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                   {
                       loader: 'postcss-loader',
                       options: {
                           postcssOptions: {
                               // postcss的插件
                               plugins: [
                                require('postcss-preset-env')(),
                               ]
                           }
                       }
                   }
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html'
        }),
        new MiniCssExtractPlugin({
            filename: 'css/built.css',
        }),
        // 压缩css
        // new OptimizeCssAssetsWebpackPlugin(),
        new CssMinimizerWebpackPlugin(),
    ],
    mode: 'development'
}